package com.share;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author:caifan
 * @date:2020/7/4
 */
public class LagouCourse {
    public static void main(String[] args) throws IOException {
        String dir = "D:\\lagou\\lesson-category\\产品体验设计思维与实操-李伟巍";
        genMd(dir,   4448, 4465);
    }

    private static void genMd(String dir, int start0, int end0) throws IOException {
        PrintWriter pw = new PrintWriter("D:/lagou/error.txt");
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }

        File[] files = file.listFiles((f, name) -> {
            if (f.isFile() && name.endsWith(".md")) {
                return true;
            }
            return false;
        });

        List<String> noList = Arrays.asList(files).stream().map(file1 -> file1.getName().substring(0, file1.getName().indexOf("-"))).collect(Collectors.toList());
        long start = System.currentTimeMillis();
        for (int i = start0; i <= end0; i++) {
            if (noList.contains(i)) {
                continue;
            }
            try {
                String content = getContent(i);
                generateFile(content, dir, i);
            } catch (Exception e) {
                pw.write("courseId" + i + ",error:" + e.getMessage() + "\n");
                pw.flush();
            }
        }
        pw.close();
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start) / 1000 + "s");
    }

    private static String getContent(Integer courseId) throws Exception {
        HttpClient client = HttpClients.createDefault();
        //第一种 java方向
        HttpGet httpGet = new HttpGet("https://gate.lagou.com/v1/neirong/kaiwu/getCourseLessonDetail?lessonId=" + courseId);
        //第二种  运营方向
//        HttpGet httpGet = new HttpGet("https://gate.lagou.com/v1/neirong/kaiwu/getCourseLessons?courseId=" + courseId);
        httpGet.addHeader("Cookie", "_ga=GA1.2.999643115.1550409501; user_trace_token=20190217211842-8bd832a1-32b6-11e9-8212-5254005c3644; LGUID=20190217211842-8bd83590-32b6-11e9-8212-5254005c3644; _putrc=7B16CC75A96D3702; login=true; unick=%E8%94%A1%E5%87%A1; X_MIDDLE_TOKEN=59515dc0ff9ded43acebb465a1322d04; sensorsdata2015session=%7B%7D; LG_HAS_LOGIN=1; smidV2=202010232240258be17a43603a0bb891443ffb5b22e610006203829f5422850; SESSION=723f8b34-1f7b-4302-b40a-2df854787807; JSESSIONID=C0D40B5C6061AEFBC74DE1547A0C6A3C; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1644646865; index_location_city=%E5%B9%BF%E5%B7%9E; LGRID=20220220223624-59a58bbd-1ef3-4203-bddb-c3b0c7d9c0e9; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1645367785; X_HTTP_TOKEN=659bc3351c610797812165646123e5f8028cf916bd; EDUJSESSIONID=ABAAAECABCAAACDC7FEAA7375C28C155CE7C6C6F3EBCB48; thirdDeviceIdInfo=%5B%7B%22channel%22%3A1%2C%22thirdDeviceId%22%3A%22WHJMrwNw1k/FsYDZ/e1ZnohIbremDlfSr0o3MGtiFCJrN/mx9O2TEkjzoA4vjLeQgGO3MsBN5oereEjBAUyj0jaHPyWLMTLXodCW1tldyDzmQI99+chXEiuI3r7WW45339lCUKKcsmkTaFO8webhNijYmmmXo8LlTkQE5YcNLqNriNYPfoOP/btUt6KtoW1kBefR0vBc79YsjgzZIaYhnXu8Vg+5pExYXfkd/2z3NgaztcDudQ8mqgRHqbB3ttveHSBMZJRjFRB4%3D1487582755342%22%7D%2C%7B%22channel%22%3A2%2C%22thirdDeviceId%22%3A%22140%23ssboJia+zzPlQzo2+iDz4pN8s7aD7qWBXSH+uPIrFY3dtknDQR4Df5mRgRX2q2Hj4QVflp1zz/7fmEPIOQzxdxHK9ph/zzrb22U3lp1xzZuVV2EqlaOz2PD+VDs3sQcI1wba7X5mUc23nMUk1ysaMwNJLu+J7B4BAQnpOkAQxuhijvLl8u1m3sW91swio6v7e7blyKvwbnlTkL1rThkE3rxay2fVm1HTQn5OnwE6Ih/6zx/PhmA0KuAi1QrdvCuqEk8zoiBTP1j9pocb6pl1o4FB/g4m1rvb5miyVPYRH2ldktWa6vUPXfVHhsbKz0TsEHJKklJ48Xji2tTjgEc/I+spFkxWLPEb6C4dKMM08H8L1RM/uQShFiN75wSkm63zOVbIBSg3WIzbTdUsUTt+v9cBhlzr4UX1/qZnzhDSk6cmAsS+O/NUdVaUNrDG5bT9ToTCMn9bdoNDwbl5BPm/LWdmsmc/W2GyP5g/cpfwjn4pWiBwcH9lD0GPOBjqjjv/Kc6DfycXUY7uZocrsYxOEK5+1F7BmSZMjW9kuBwamt/SUSCtfKGHwf7WUvFSX0HFP61HvUY6rseoXk5gBphHYuN8LyL1gFGoaOYyDGulAiTxUxQwEiGXBhrV22o48zWJSmAYgafK9MLOljfdHGq7UgdB8ILQOHIdXzxH77uEaC/XmBsTgnToUS9dirPDsoOS2LhqhFwWIPNyN11Kdvn2RC+koGECrEC9lTEFu+TaIhXceYnQLk4y+mbvGPiitWRUi8h01MIyT9PyuxTRJz4X+ylTNubpajkj/Z4YujzbdN8y3BKoii5CoVUMGoUvYxQSJUz9egbF+ZUlLebvB/fANx5NFv3kVROAduJtQt7POQRHqX8U0OBqKrYzboB5mc4dIK54FryLKfoPet9EQ7MbILaVgycDYcVt2Tuopjpio0wLzt2UVt+aAltZJL7aNpN9ZKmfQL4pWCC6fUXwotipJkTvHrWnS1vWdzvs1Vvkdqz83BdvnKp5%2Cundefined%22%7D%5D; user-finger=4b479af673282de1bda17336540bfbb5; gate_login_token=b162ef5f93cd64525af7f2c2ad82ad6dc4b8f7fb26099015; LG_LOGIN_USER_ID=7ee820e734dc6a852a90a530e7c06f7312b3349c402f82d1; edu_gate_login_token=$$$EDU_eyJraWQiOiI3MzM3ZDZkZi1lNDNlLTRkZTMtYWE1Ny05OWY5ZTZjODliYjIiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ0Z3QiOiJfQ0FTX1RHVF9UR1QtNGViZjQ5MmU5ZTJlNGYzNDlmMDRjZjE3NTE5M2QyOGItMjAyMjAzMDcyMzE1NDgtX0NBU19UR1RfIiwic3ViIjoiMzU1MDQ4IiwiaXAiOiIyMjMuNzMuMjE4LjEwIiwiaXNzIjoiZWR1LmxhZ291LmNvbSIsInRva2VuVHlwZSI6MSwiZXhwIjoxNjQ3MjcwOTQ4LCJpYXQiOjE2NDY2NjYxNDh9.C2_OuY2B2brdB-lkB6CfFypxrAUD5boT0kDYai7uIndZ7PhOH52hUwkWe_K_dzMJTfRQRocofVtsux40UcOze1fBm9dV8ncDFMKai4LRo3ANM1k5O6yJidqgSNVJfngCWAGWBCuqAkQRvIVC5ZVHZQuvRsuGI6WR0tmHxZShR5JwKHMoG8Fb14Fz2xFgnyt97khzNokxTWXSxWLzoKcvH5wF_0A5ZXjY4uRlz4Tr0SS6VJYH99NCqgzBWuOdk5diAnHCq98e1dj3xxvbw0oW8avvpYRpY2Xg4E-GG7KaZh79WaFcqWXEGwtetm3RQmIw5E98duG8B3M4lzvvWw-wFA; kw_login_authToken=\"MdJtVc+acY4gyD51H6DYYBJP2IHAwLNCHnuTZJP+uQ8/TGPD66rlRiRThMm3VP990PiEAfc38VVUyN/1K8vTU/XVuKgTc9FtUa4cTEcgdpVLP3np6tcINn8/iHGzKSoyLGg96DmSxgKmyY0HeAHaRrBCSFfmhTy8fqJ5MBi7h3t4rucJXOpldXhUiavxhcCELWDotJ+bmNVwmAvQCptcy5e7czUcjiQC32Lco44BMYXrQ+AIOfEccJKHpj0vJ+ngq/27aqj1hWq8tEPFFjdnxMSfKgAnjbIEAX3F9CIW8BSiMHYmPBt7FDDY0CCVFICHr2dp5gQVGvhfbqg7VzvNsw==\"; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22355048%22%2C%22%24device_id%22%3A%22168fb9bbcb7782-0cd9233083a6f6-5c11301c-1327104-168fb9bbcb89c1%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24os%22%3A%22Windows%22%2C%22%24browser%22%3A%22Chrome%22%2C%22%24browser_version%22%3A%2299.0.4844.51%22%7D%2C%22first_id%22%3A%2217869cfbfc626e-03a45e2bf0fb41-353c540f-1327104-17869cfbfc77d9%22%7D");
        httpGet.addHeader("x-l-req-header", "{deviceType:1}");
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        InputStream inputStream = entity.getContent();
        return IOUtils.toString(inputStream);
    }

    /**
     * 生成文件
     * @param content
     * @throws Exception
     */
    private static void  generateFile(String content, String dir, Integer no) throws Exception {
        JSONObject contentObj = JSONObject.parseObject(content);
        //content节点
        JSONObject content1 = contentObj.getJSONObject("content");

        String title = content1.getString("theme");//标题
        title = title.replace("|", "-");
        title = title.replace(":", "-");
        title = title.replace("：", "-");
        title = title.replace("/", "-");
        title = title.replace("\\", "-");
        title = title.replace("*", "-");
        title = title.replace("?", "");
        title = title.replace("？", "");
        // 视频地址
        JSONObject videoJSON = content1.getJSONObject("videoMedia");
        String videoUrl = videoJSON.getString("fileUrl");

        //音频地址
        JSONObject audioJSON = content1.getJSONObject("audioMedia");
        String audioUrl = audioJSON.getString("fileUrl");

        //真实有用的内容
        String realContent = content1.getString("textContent");

        PrintWriter printWriter = new PrintWriter(dir + File.separator + title + ".md");
        printWriter.write("标题：" + title + "\n<br>");
//        if (StringUtils.isNotEmpty(videoUrl)) {
//            printWriter.write("视频地址：" + videoUrl + "\n<br>");
//        }
//        printWriter.write("音频地址：" + audioUrl);
        printWriter.write(realContent);
        printWriter.flush();
        printWriter.close();
    }
}
